package com.group.three.controller.orderController;


import com.group.three.sdk.WXPayUtil;
import com.group.three.sdk.WxPayService;
import com.group.three.service.myorderservice.VxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * * 买家端，微信支付接口
 */
@Api(tags = "买家端,微信支付接口")
@RestController
@RequestMapping("/wx")
@Slf4j
@Data
public class WxController {
    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private VxPayService vxPayService;
    private String orderId = "";

    @ApiOperation(value = "微信支付信息接口")
    @GetMapping("/doNativePay")
    public Map<String, Object> doNativePay(String orderId) {
        this.orderId = orderId;
        log.info("开始微信订单支付 orderId={}", orderId);
        try {
            Map<String, Object> res = wxPayService.nativePay(orderId, 1);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }

    }

    @RequestMapping("/index")
    public ModelAndView toIndex(ModelAndView modelAndView) {
        modelAndView.setViewName("WxQrcode");
        return modelAndView;
    }

    @ApiOperation(value = "微信支付回调接口")
    @RequestMapping("/receiveWxPay")
    @ResponseBody
    public String receiveWxPay(HttpServletRequest request,
                               HttpServletResponse response) {
        System.out.println("微信已经调用回调接口");
        try {
            ServletInputStream inputStream = request.getInputStream();
            String callback = inputStream2String(inputStream, "utf-8");
            Map<String, String> map = WXPayUtil.xmlToMap(callback);
            System.out.println("111"+map);
            System.out.println(this.orderId);
            if (map.get("return_code").equals("SUCCESS")) {
                int i = vxPayService.vxPay(orderId);
            }
            map.clear();
            map.put("return_code", "SUCCESS");
            map.put("return_msg", "OK");
            return WXPayUtil.mapToXml(map);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String inputStream2String(InputStream inStream, String encoding) {
        String result = null;
        ByteArrayOutputStream outStream = null;
        try {
            if (inStream != null) {
                outStream = new ByteArrayOutputStream();
                byte[] tempBytes = new byte[1024];
                int count = -1;
                while ((count = inStream.read(tempBytes, 0, 1024)) != -1) {
                    outStream.write(tempBytes, 0, count);
                }
                tempBytes = null;
                outStream.flush();
                result = new String(outStream.toByteArray(), encoding);
                outStream.close();
            }
        } catch (Exception e) {
            result = null;
        } finally {
            try {
                if (inStream != null) {
                    inStream.close();
                    inStream = null;
                }
                if (outStream != null) {
                    outStream.close();
                    outStream = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @ApiOperation(value = "微信支付结果接口")
    @GetMapping("/vxPayResult")
    public String vxPayResult(String sn) {
        String PayResult = vxPayService.vxResult(sn);
        if (PayResult.equals("PAID")) {
            return "success";
        } else {
            return "fail";
        }

    }
}
